﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

using System.Data.OracleClient;

namespace CommonLibrary.Data.NetOracle
{
    public class NetOracleDefault
    {
        public static string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["OracleConnectionString"];

        #region ExecuteNonQuery

        public static int ExecuteNonQuery(CommandType cmdType, string in_sql, OracleParameter[] in_paras)
        {
            return ClNetOracle.ExecuteNonQuery(ConnectionString, cmdType, in_sql, in_paras);
        }

        public static int ExecuteNonQuery(CommandType cmdType, string in_sql)
        {
            return ExecuteNonQuery(cmdType, in_sql, null);
        }

        public static int ExecuteNonQuery(string in_sql, OracleParameter[] in_paras)
        {
            return ExecuteNonQuery(CommandType.Text, in_sql, in_paras);
        }

        public static int ExecuteNonQuery(string in_sql)
        {
            return ExecuteNonQuery(in_sql, null);
        }


        #endregion


        #region ExecuteScalar

        public static object ExecuteScalar(CommandType cmdType, string in_sql, OracleParameter[] in_paras)
        {
            return ClNetOracle.ExecuteScalar(ConnectionString, cmdType, in_sql, in_paras);
        }

        public static object ExecuteScalar(CommandType cmdType, string in_sql)
        {
            return ExecuteScalar(cmdType, in_sql, null);
        }

        public static object ExecuteScalar(string in_sql, OracleParameter[] in_paras)
        {
            return ExecuteScalar(CommandType.Text, in_sql, in_paras);
        }

        public static object ExecuteScalar(string in_sql)
        {
            return ExecuteScalar(in_sql, null);
        }

        #endregion


        #region ExecuteDataSet

        public static DataSet ExecuteDataSet(CommandType cmdType, string in_sql, OracleParameter[] in_paras)
        {
            return ClNetOracle.ExecuteDataSet(ConnectionString, cmdType, in_sql, in_paras);
        }

        public static DataSet ExecuteDataSet(CommandType cmdType, string in_sql)
        {
            return ExecuteDataSet(cmdType, in_sql, null);
        }

        public static DataSet ExecuteDataSet(string in_sql, OracleParameter[] in_paras)
        {
            return ExecuteDataSet(CommandType.Text, in_sql, in_paras);
        }

        public static DataSet ExecuteDataSet(string in_sql)
        {
            return ExecuteDataSet(in_sql, null);
        }



        #endregion


        #region ExecuteDataTable

        public static DataTable ExecuteDataTable(CommandType cmdType, string in_sql, OracleParameter[] in_paras)
        {
            return ClNetOracle.ExecuteDataTable(ConnectionString, cmdType, in_sql, in_paras);
        }

        public static DataTable ExecuteDataTable(CommandType cmdType, string in_sql)
        {
            return ExecuteDataTable(cmdType, in_sql, null);
        }

        public static DataTable ExecuteDataTable(string in_sql, OracleParameter[] in_paras)
        {
            return ExecuteDataTable(CommandType.Text, in_sql, in_paras);
        }

        public static DataTable ExecuteDataTable(string in_sql)
        {
            return ExecuteDataTable(in_sql, null);
        }

        #endregion


        #region ExecuteReader 用完后一定要使用reader.Close

        public static OracleDataReader ExecuteReader(CommandType cmdType, string in_sql, OracleParameter[] in_paras)
        {
            return ClNetOracle.ExecuteReader(ConnectionString, cmdType, in_sql, in_paras);
        }

        public static OracleDataReader ExecuteReader(CommandType cmdType, string in_sql)
        {
            return ExecuteReader(cmdType, in_sql, null);
        }

        public static OracleDataReader ExecuteReader(string in_sql, OracleParameter[] in_paras)
        {
            return ExecuteReader(CommandType.Text, in_sql, in_paras);
        }

        public static OracleDataReader ExecuteReader(string in_sql)
        {
            return ExecuteReader(in_sql, null);
        }

        #endregion


        #region ExecuteNonQueryTransaction

        public static int ExecuteNonQueryTransaction(CommandType cmdType, string in_sql, OracleParameter[] in_paras, bool in_commit)
        {
            return ClNetOracle.ExecuteNonQueryTransaction(ConnectionString, cmdType, in_sql, in_paras, in_commit);
        }

        public static int ExecuteNonQueryTransaction(CommandType cmdType, string in_sql, OracleParameter[] in_paras)
        {
            return ExecuteNonQueryTransaction(cmdType, in_sql, in_paras, true);
        }

        public static int ExecuteNonQueryTransaction(CommandType cmdType, string in_sql)
        {
            return ExecuteNonQueryTransaction(cmdType, in_sql, null);
        }

        public static int ExecuteNonQueryTransaction(string in_sql, OracleParameter[] in_paras, bool in_commit)
        {
            return ExecuteNonQueryTransaction(CommandType.Text, in_sql, in_paras, in_commit);
        }

        public static int ExecuteNonQueryTransaction(string in_sql, OracleParameter[] in_paras)
        {
            return ExecuteNonQueryTransaction(in_sql, in_paras, true);
        }

        public static int ExecuteNonQueryTransaction(string in_sql)
        {
            return ExecuteNonQueryTransaction(in_sql, null, true);

        }

        public static bool ExecuteNonQueryTransaction(Dictionary<string, OracleParameter[]> listParams)
        {
            using (OracleConnection cn = new OracleConnection(ConnectionString))
            {
                cn.Open();
                OracleTransaction tran = cn.BeginTransaction();
                try
                {
                    foreach (KeyValuePair<string, OracleParameter[]> kv in listParams)
                    {
                        tran.ClExecuteNonQuery(kv.Key, kv.Value);
                    }
                    tran.Commit();
                    return true;

                }
                catch
                {
                    tran.Rollback();
                    return false;
                }
                finally
                {
                    cn.Close();
                }

            }
        }

        #endregion


        #region CheckIsEmpty

        /// <summary>
        /// 判断SQL命令查询的结果是否存在
        /// </summary>
        /// <param name="in_sql"></param>
        /// <param name="in_paras"></param>
        /// <returns></returns>
        public static bool CheckIsEmpty(string in_sql, OracleParameter[] in_paras)
        {
            return ClNetOracle.CheckIsEmpty(ConnectionString, in_sql, in_paras);
        }

        /// <summary>
        /// 判断SQL命令查询的结果是否为空
        /// </summary>
        /// <param name="in_sql"></param>
        /// <returns></returns>
        public static bool CheckIsEmpty(string in_sql)
        {
            return CheckIsEmpty(in_sql, null);
        }

        #endregion


        #region IsCanDelete

        /// <summary>
        /// 是否能删除数据
        /// </summary>
        /// <returns>true:能删除数据  ,false:不能删除数据</returns>
        public static bool IsCanDelete(string deleteSql, params OracleParameter[] cmdParms)
        {
            return ClNetOracle.IsCanDelete(ConnectionString, deleteSql, cmdParms);

        }

        #endregion


        #region GetPageData

        /// <summary>
        /// 分页处理
        /// </summary>
        /// <param name="in_Sql">sql命令</param>
        /// <param name="in_paras">sql参数</param>
        /// <param name="in_PageSize">一页取多少行</param>
        /// <param name="in_PageNum">取第几页</param>
        /// <param name="out_Rows">返回总行数</param>
        /// <param name="out_Pages">返回总页数</param>
        /// <param name="dropRowID">是否删除row_number排序号</param>
        /// <returns></returns>
        public static DataTable GetPageData(string in_Sql, OracleParameter[] in_paras, int in_PageSize, int in_PageNum, bool dropRowID, out int out_Rows, out int out_Pages)
        {
            return ClNetOracle.GetPageData(ConnectionString, in_Sql, in_paras, in_PageSize, in_PageNum, dropRowID, out  out_Rows, out  out_Pages);
        }

        /// <summary>
        /// 分页处理
        /// </summary>
        /// <param name="in_Sql">sql命令</param>
        /// <param name="in_paras">sql参数</param>
        /// <param name="in_PageSize">一页取多少行</param>
        /// <param name="in_PageNum">取第几页</param>
        /// <param name="out_Rows">返回总行数</param>
        /// <param name="out_Pages">返回总页数</param>
        /// <returns></returns>
        public static DataTable GetPageData(string in_Sql, OracleParameter[] in_paras, int in_PageSize, int in_PageNum, out int out_Rows, out int out_Pages)
        {
            return ClNetOracle.GetPageData(ConnectionString, in_Sql, in_paras, in_PageSize, in_PageNum, true, out out_Rows, out out_Pages);
        }

        /// <summary>
        /// 分页处理
        /// </summary>
        /// <param name="in_Sql">sql命令</param>
        /// <param name="in_PageSize">一页取多少行</param>
        /// <param name="in_PageNum">取第几页</param>
        /// <param name="out_Rows">返回总行数</param>
        /// <param name="out_Pages">返回总页数</param>
        /// <returns></returns>
        public static DataTable GetPageData(string in_Sql, int in_PageSize, int in_PageNum, out int out_Rows, out int out_Pages)
        {
            return ClNetOracle.GetPageData(ConnectionString, in_Sql, null, in_PageSize, in_PageNum, true, out out_Rows, out out_Pages);
        }

        #endregion

    }

}
